package com.hava.contentsale.web.controller;

import com.hava.contentsale.meta.ContentPO;
import com.hava.contentsale.meta.ProductVO;
import com.hava.contentsale.meta.UserVO;
import com.hava.contentsale.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by yanfa on 2016/11/20.
 */
@Controller
public class PublicController {

    @Autowired
    ProductService productServiceImpl;

//    R6 发布
//    R6.1 卖家在展示界面点击“发布”按钮，进入发布界面 - 在有卖家的header内容均会展示发布
//    R6.2 在发布界面可以输入内容的标题、摘要、图片、正文和价格，点击发布按钮即发布，回到查看界面。

    //展示发布界面
    @RequestMapping(value = "/public")
    public ModelAndView public_view(HttpSession httpSession)
    {
        System.out.println("Class PublicController Method public_view");
        //session
        System.out.println("[session.id]:" + httpSession.getId());
        UserVO userVO = (UserVO) httpSession.getAttribute("user");

        //当用户没有登录时，跳转回登录界面
        if(userVO == null)
            return new ModelAndView("redirect:login");
            //如果用户类型为卖家，则跳转到发布界面
        else if(userVO.getUsertype() == 1)
        {
            ModelAndView modelAndView = new ModelAndView("public");
            //当用户不为空时，返回用户信息
            if (userVO != null)
                modelAndView.addObject("user", userVO);

            return modelAndView;
        }
        //由于主访问界面没有错误提示处理，则其他用户类型，直接跳转登录界面
        else
            return new ModelAndView("redirect:login");
    }

    //提交发布内容：如下内容为其中一次的提交示例
    //http://localhost:8080/content_sale/publicSubmit
    //    title:中文标题
    //    summary:中文摘要
    //    image:http://www.baidu.com/img/bd_logo1.png
    //    detail:正文123
    //    price:123.12
    @RequestMapping(value = "/publicSubmit",method = RequestMethod.POST)
    public ModelAndView publiSubmit(
            @RequestParam("title") String title,
            @RequestParam("image") String image,
            @RequestParam("detail") String detail,
            @RequestParam("summary") String summary,
            @RequestParam("price") String price,
            HttpSession httpSession)
    {
        System.out.println("Class PublicController Method publiSubmit");
        //session
        System.out.println("[session.id]:" + httpSession.getId());
        UserVO userVO = (UserVO) httpSession.getAttribute("user");

        //当用户没有登录时，跳转回登录界面
        if(userVO == null)
            return new ModelAndView("redirect:login");
            //如果用户类型为卖家，则继续处理
        else if(userVO.getUsertype() == 1)
        {

            ModelAndView modelAndView = new ModelAndView("publicSubmit");
            //当用户不为空时，返回用户信息
            if (userVO != null)
                modelAndView.addObject("user", userVO);

            ProductVO productVO = new ProductVO();
            productVO.setTitle(title);
            productVO.setImage(image);
            productVO.setSummary(summary);
            productVO.setPrice(new BigDecimal(price));
            productVO.setDetail(detail);
            productVO.printProperties();

            this.productServiceImpl.create(productVO);
            productVO.printProperties();

            modelAndView.addObject("product",productVO);

            return modelAndView;
        }
        //由于主访问界面没有错误提示处理，则其他用户类型，直接跳转登录界面
        else
            return new ModelAndView("redirect:login");
    }
}
